package com.ctrl.riveradministrator.mvp.view.activity.learn_zoom;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.base.BaseViewHolder;
import com.ctrl.riveradministrator.common.Constants;
import com.ctrl.riveradministrator.mvp.model.LearnZoom;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

public class LearnZoomAdapter extends BaseListAdapter<LearnZoom> {

    private Context context;
    private TextView bulletinName, phone, people;
    private ImageView levelIv;
    private RelativeLayout content;
    private SimpleDraweeView imageView;
    private GenericDraweeHierarchyBuilder builder;
    private GenericDraweeHierarchy hierarchy;
    private ImageRequest request;
    private DraweeController controller;

    public LearnZoomAdapter(Context mContext) {
        super(mContext);
        this.context = mContext;
        builder = new GenericDraweeHierarchyBuilder(context.getResources());
    }

    @Override
    public int getLayoutId() {
        return R.layout.learnzoom_item;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindItemHolder(BaseViewHolder holder, int position) {

        bulletinName = holder.getView(R.id.bulletin_name_tv);
        people = holder.getView(R.id.people_tv);
        content = holder.getView(R.id.content);
        imageView = holder.getView(R.id.bulletin_iv);

        LearnZoom learnZoom = getDataList().get(position);

        learnZoom.setPath(learnZoom.getLearnDetalis());

        bulletinName.setText(learnZoom.getLearnBiaoti());
        people.setText(learnZoom.getLearnContent());
        content.setOnClickListener(v -> mOnItemClickListener.onItemClick(learnZoom, position));
        Uri uri = Uri.parse(Constants.BASE_URL_ + learnZoom.getPath());
        hierarchy = imageView.getHierarchy();
        hierarchy.setFailureImage(R.mipmap.ic_launcher);
        hierarchy.setProgressBarImage(new ProgressBarDrawable());
        //imageView.setImageURI(uri);
        if (learnZoom.getPath() == null || learnZoom.getPath().equals("")) {
            return;
        }
        request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(100, 100))
                .build();
        controller = Fresco.newDraweeControllerBuilder()
                .setOldController(imageView.getController())
                .setImageRequest(request)
                .build();
        imageView.setController(controller);
    }


}
